// pages/other//choudetail/index.js
const Page = require('../../../utils/ald-stat.js').Page;
const util = require('../../../utils/throttle.js');
const app = getApp();
let videoAd = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isInit: false,
    id: '',
    showView: false,
    norelibi: true,
    freechou: true,
    luck: true,
    bmsuc: true,
    clickedsuc: true,
    percent: 5,
    lqsuc: true,
    dikuang: true,
    zlfail: true,
    zlsuc: true,
    content: '',
    sort: '',
    friendava: '',
    ad_auth: '',
    ad_tims: '',
    currency: '',
    ad_id: '',
    ad_data: '',
    flow: false,
    erweima: '',
    clickedfail: true,
    tryfail: true,
    zhongStatus: '', //中奖状态
    level: '',
    get_win_list: '',
    i: '',
    zhongType: '', //中奖类型、实物还是币
    address: '',
    remarks: '',
    plate: '',
    video_ad: true,
    first_win_list:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.showLoading({
      title: '加载中',
    })
    const that = this;
    const id = options.id;
    that.setData({
      id: id
    })
    if (options.openid && options.share == "gl") {
      var objet = {
        unionid: options.openid,
        id: id,
        hyunionid: wx.getStorageSync('openid')
      }
      var sin = app.sort_ASCII(objet)
      wx.request({
        url: app.globalData.new_url + '/luck/luckydraw/helpehand',
        data: {
          unionid: options.openid,
          id: id,
          hyunionid: wx.getStorageSync('openid'),
          sign: sin
        },
        method: "POST",
        header: {
          'content-type': 'application/json'
        },
        success: function(res) {
          console.log('助力', res)
          if (res.data.code === 200) {
            that.setData({
              zlsuc: false
            })
          } else {
            that.setData({
              zlfail: false,
            })
          }
        }
      })
    }
    if (wx.createRewardedVideoAd) {
      videoAd = wx.createRewardedVideoAd({
        adUnitId: 'adunit-5d4fa1a37693840f'
      });
      videoAd.load();
      videoAd.onError(function(res) {
        console.log(101, res)
        if (res.errMsg == 'no advertisement' || res.errMsg == 'no ad unit id' || res.errCode == 1004) {
          that.setData({
            video_ad: false,
          })
        }
      })
      videoAd.onClose((status) => {
        if (status && status.isEnded) {
          var plate = that.data.plate;
          var luckyid=that.data.id;
          var obj = {
            unionid: wx.getStorageSync('openid'),
            plate: plate,
            luckyid: luckyid,
          }
          var sign = app.sort_ASCII(obj)
          wx.request({
            url: app.globalData.new_url + '/adauth/Advertising/video_ad',
            data: {
              unionid: wx.getStorageSync('openid'),
              luckyid: luckyid,
              plate: plate,
              sign: sign
            },
            method: 'POST',
            success: function(res) {
              console.log('激励视频体验广告', res)
              if (res.data.code === 200) {
                if (plate == 9) {
                  var objs = {
                    unionid: wx.getStorageSync('openid'),
                    luckyid: luckyid,
                  }
                  var signs = app.sort_ASCII(objs)
                  wx.request({
                    url: app.globalData.new_url + '/luck/Luckydraw/get_probability',
                    data: {
                      unionid: wx.getStorageSync('openid'),
                      luckyid: luckyid,
                      sign: signs
                    },
                    method: 'POST',
                    success: function(res) {
                      console.log('视频激励广告体验报名', res)
                      that.setData({
                        sort: res.data.content.sort,
                        freechou: true,
                        bmsuc: false,
                        clickedsuc: true,
                      },()=>{
                        that.onShow();
                      })
                    }
                  })
                }else{
                  that.onShow();
                }
              }
            }
          })
        } else {
          return
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },
  imageLoad: function(e) {
    wx.hideLoading();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    const that = this;
    var ojb = {
      unionid: wx.getStorageSync('openid'),
    }
    var sign = app.sort_ASCII(ojb)
    wx.request({
      url: app.globalData.new_url + '/luck/luckydraw/get_status',
      data: {
        unionid: wx.getStorageSync('openid'),
        sign: sign
      },
      method: 'POST',
      success: function(res) {
        console.log('获取广告状态', res)
        that.setData({
          ad_auth: res.data.ad_auth,
          ad_tims: res.data.ad_tims,
          currency: res.data.currency,
          ad_id: res.data.ad_id,
        })
      }
    })
    var luckyid = that.data.id
    if (that.data.erweima == 3) {
      if (that.data.plate == 9) {
        var obj = {
          unionid: wx.getStorageSync('openid'),
          id: luckyid,
          type: 1,
        }
        var sign = app.sort_ASCII(obj)
        wx.request({
          url: app.globalData.new_url + '/luck/luckydraw/sign_up',
          data: {
            unionid: wx.getStorageSync('openid'),
            id: luckyid,
            type: 1,
            sign: sign
          },
          method: 'POST',
          success: function(res) {
            console.log('二维码体验成功', res)
            if (res.data.code === 200) {
              var objs = {
                unionid: wx.getStorageSync('openid'),
                luckyid: luckyid,
              }
              var signs = app.sort_ASCII(objs)
              wx.request({
                url: app.globalData.new_url + '/luck/Luckydraw/get_probability',
                data: {
                  unionid: wx.getStorageSync('openid'),
                  luckyid: luckyid,
                  sign: signs
                },
                method: 'POST',
                success: function(res) {
                  console.log('自接广告体验报名', res)
                  that.setData({
                    sort: res.data.content.sort,
                    freechou: true,
                    bmsuc: false,
                    clickedsuc: true,
                  },()=>{
                    that.onShow();
                  })
                }
              })
            }
          }
        })
      }
      that.data.erweima = 0
    } else if (that.data.erweima == 4) {
      that.setData({
        clickedfail: true,
        tryfail: false,
      })
      that.data.erweima = 0
    }
    if (app.globalData.scene === 1038 && that.data.flow) {
      var ad_id = that.data.ad_id
      console.log(ad_id)
      var plate = that.data.plate
      var objet = {
        unionid: wx.getStorageSync('openid'),
        id: ad_id,
        luckyid: luckyid,
        type: 2,
        plate: plate,
      }
      var sin = app.sort_ASCII(objet)
      wx.request({
        url: app.globalData.new_url + '/adauth/Advertising/oneself_ad',
        data: {
          unionid: wx.getStorageSync('openid'),
          type: 2,
          id: ad_id,
          plate: plate,
          luckyid: luckyid,
          sign: sin
        },
        method: "POST",
        header: {
          'content-type': 'application/json'
        },
        success: function(res) {
          console.log('广告回调成功', res)
          app.globalData.scene = 1001;
          that.data.erweima = 0;
          if (res.data.code == 200) {
            if (plate == 9) {
              var obj = {
                unionid: wx.getStorageSync('openid'),
                id: luckyid,
                type: 1,
              }
              var sign = app.sort_ASCII(obj)
              wx.request({
                url: app.globalData.new_url + '/luck/luckydraw/sign_up',
                data: {
                  unionid: wx.getStorageSync('openid'),
                  id: luckyid,
                  type: 1,
                  sign: sign
                },
                method: 'POST',
                success: function(res) {
                  console.log('直接跳转报名成功', res)
                  if (res.data.code === 200) {
                    var objs = {
                      unionid: wx.getStorageSync('openid'),
                      luckyid: luckyid,
                    }
                    var signs = app.sort_ASCII(objs)
                    wx.request({
                      url: app.globalData.new_url + '/luck/Luckydraw/get_probability',
                      data: {
                        unionid: wx.getStorageSync('openid'),
                        luckyid: luckyid,
                        sign: signs
                      },
                      method: 'POST',
                      success: function(res) {
                        console.log('自接广告体验报名成功', res)
                        that.setData({
                          sort: res.data.content.sort,
                          freechou: true,
                          bmsuc: false,
                          clickedsuc: true,
                        },()=>{
                          that.onShow();
                        })
                      }
                    })
                  }
                }
              })
            } else {
              that.onShow();
            }
          } else {
            that.setData({
              flow:false,
              tryfail: false,
              clickedfail: true
            })
          }
        }
      })
    }
    var obj = {
      unionid: wx.getStorageSync('openid'),
      id: luckyid
    }
    var sign = app.sort_ASCII(obj)
    wx.request({
      url: app.globalData.new_url + '/luck/Luckydraw/detail',
      data: {
        unionid: wx.getStorageSync('openid'),
        id: luckyid,
        sign: sign
      },
      method: 'POST',
      success: function(res) {
        console.log('抽奖详情页', res)
        that.setData({
          content: res.data.content,
        })
        if (res.data.content.status === 1 && res.data.content.sign_up === 1) {
          var objs = {
            unionid: wx.getStorageSync('openid'),
            luckyid: luckyid
          }
          var signs = app.sort_ASCII(objs)
          wx.request({
            url: app.globalData.new_url + '/luck/Luckydraw/get_probability',
            data: {
              unionid: wx.getStorageSync('openid'),
              luckyid: luckyid,
              sign: signs
            },
            method: 'POST',
            success: function(res) {
              console.log('抽奖概率列表', res)
              if (res.data.content.probability >= 80) {
                var percent = 80
              } else {
                var percent = res.data.content.probability
              }
              that.setData({
                percent: percent,
                sort: res.data.content.sort,
                isInit: true
              })
            }
          })
          wx.request({
            url: app.globalData.new_url + '/luck/Luckydraw/helpehand_list',
            data: {
              unionid: wx.getStorageSync('openid'),
              luckyid: luckyid,
              sign: signs
            },
            method: 'POST',
            success: function(res) {
              console.log('好友助力列表', res)
              if (res.data.code === 200) {
                that.setData({
                  friendava: res.data.content,
                })
              }
            }
          })
        } else if (res.data.content.status === 3 || res.data.content.status === 2 || res.data.content.status === 4) {
          var objss = {
            unionid: wx.getStorageSync('openid'),
            lucky_id: luckyid
          }
          var signss = app.sort_ASCII(objss)
          wx.request({
            url: app.globalData.new_url + '/assistant/Assistant/get_state',
            data: {
              unionid: wx.getStorageSync('openid'),
              lucky_id: luckyid,
              sign: signss
            },
            method: 'POST',
            success: function (res) {
              console.log('获取用户中奖状态', res)
              that.setData({
                zhongStatus: res.data.content.status,
                zhongType: res.data.content.type,
                isInit: true
              })
            }
          })
          var objet = {
            luckyid: luckyid
          }
          var sgn = app.sort_ASCII(objet)
          wx.request({
            url: app.globalData.new_url + '/luck/Luckydraw/get_win_list',
            data: {
              luckyid: luckyid,
              sign: sgn
            },
            method: 'POST',
            success: function(res) {
              console.log('中奖列表', res)
              if(res.data.code===200){
                that.setData({
                  get_win_list: res.data.content
                })
                var unionid = wx.getStorageSync('openid');
                var first_win_list = [];
                for (var i = 0; i < res.data.content.length; i++) {
                  if (unionid == res.data.content[i].unionid) {
                    var level = res.data.content[i].level
                    that.setData({
                      level: level
                    })
                  }
                  if (res.data.content[i].level === 1) {
                    first_win_list.push(i);
                    that.setData({
                      first_win_list: first_win_list
                    })
                    if (that.data.content.first_num === 0 && (res.data.content[i].unionid == wx.getStorageSync('openid')) ){
                      that.setData({
                        zhongStatus:4
                      })
                    }
                  }
                }
              }
            }
          })
        } else {
          that.setData({
            isInit: true
          })
        }
      }
    })
  },
  /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
  onPullDownRefresh: function () {
    let that = this;
    wx.showNavigationBarLoading() //在标题栏中显示加载
    that.onShow(); // 刷新页面
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  onHide: function() {
    app.globalData.scene = 1001;
  },
  openluck: util.throttle(function(e) {
    const that = this;
    videoAd.show().catch(err => {
      //失败重试
      videoAd.load().then(() => {
        videoAd.show();
      })
    })
    that.setData({
      luck: true,
    })
  }, 1500),
  //获取自接广告
  getAdd: function() {
    const that = this;
    var ad_id = that.data.ad_id;
    var obj = {
      unionid: wx.getStorageSync('openid'),
      block: 9,
      ad_id: ad_id
    }
    var sign = app.sort_ASCII(obj)
    wx.request({
      url: app.globalData.new_url + '/adauth/Adv/get_ad',
      data: {
        unionid: wx.getStorageSync('openid'),
        ad_id: ad_id,
        block: 9,
        sign: sign
      },
      method: 'POST',
      success: function(res) {
        console.log(res)
        if (res.data.code === 200) {
          that.setData({
            ad_data: res.data.content,
            freechou: true,
            luck: false,
          })
        } else if (that.data.video_ad){
          that.setData({
            freechou: true,
            luck: false,
          })
        }
      }
    })
  },
  getchance: function(e) {
    const that = this;
    that.data.plate = e.target.dataset.plate;
    that.getAdd();
  },
  enjoyjob: function(e) {
    const that = this;
    that.data.plate = e.target.dataset.plate;
    that.getAdd();
  },
  navigateToAd: function(e) {
    const that = this;
    const ad_data = that.data.ad_data;
    const ad_id = ad_data.ad_id;
    const luckyid = that.data.id;
    var plate = that.data.plate;
    var obj = {
      unionid: wx.getStorageSync('openid'),
      id: ad_id,
      luckyid: luckyid,
      type: 1,
      plate: plate,
    }
    var sign = app.sort_ASCII(obj)
    if (ad_data.way == 1) {
      wx.navigateToMiniProgram({
        appId: ad_data.appid,
        path: ad_data.path,
        envVersion: 'develop',
        success(res) {
          wx.request({
            url: app.globalData.new_url + '/adauth/Advertising/oneself_ad',
            data: {
              unionid: wx.getStorageSync('openid'),
              type: 1,
              plate: plate,
              id: ad_id,
              luckyid: luckyid,
              sign: sign
            },
            method: "POST",
            header: {
              'content-type': 'application/json'
            },
            success: function(res) {
              console.log('跳转成功', res)
              that.setData({
                luck: true
              })
              that.data.flow = true;
            }
          })
        }
      })
    } else {
      wx.navigateTo({
        url: '../../other/erweima/index?ad_id=' + ad_id + '&&plate=' + plate + '&&luckyid=' + luckyid,
      })
      that.setData({
        freechou: true,
        luck: true,
      })
    }
  },
  continueplay: function() {
    const that = this;
    that.setData({
      clickedfail: false,
      tryfail: true,
      luck: false,
    })
  },
  affirm: function() {
    const that = this;
    const lucky_id = that.data.id;
    const type = that.data.zhongType;
    var remarks = that.data.remarks;
    var address = that.data.address;
    if (address.phone && address.address && address.name) {
      var obj = {
        unionid: wx.getStorageSync('openid'),
        lucky_id: lucky_id,
        type: type,
        phone: address.phone,
        address: address.address,
        name: address.name,
        detail_address: address.detail_address,
        remarks: remarks,
      }
      var sign = app.sort_ASCII(obj)
      wx.request({
        url: app.globalData.new_url + '/assistant/Assistant/reward',
        data: {
          unionid: wx.getStorageSync('openid'),
          lucky_id: lucky_id,
          type: type,
          phone: address.phone,
          name: address.name,
          address: address.address,
          detail_address: address.detail_address,
          remarks: remarks,
          sign: sign
        },
        method: 'POST',
        success: function(res) {
          console.log(res)
          if (res.data.code === 200) {
            that.setData({
              dikuang: true,
              lqsuc: false,
            })
          }
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '请填写地址',
        showCancel: false,
      })
    }
  },
  accept: function() {
    const that = this;
    const lucky_id = that.data.id;
    const type = that.data.zhongType;
    if (type == 4) {
      var obj = {
        unionid: wx.getStorageSync('openid'),
        lucky_id: lucky_id,
        type: type
      }
      var sign = app.sort_ASCII(obj)
      wx.request({
        url: app.globalData.new_url + '/assistant/Assistant/reward',
        data: {
          unionid: wx.getStorageSync('openid'),
          lucky_id: lucky_id,
          type: type,
          sign: sign
        },
        method: 'POST',
        success: function(res) {
          console.log(res)
          if (res.data.code === 200) {
            that.onShow();
            that.setData({
              lqsuc: false,
            })
          }
        }
      })
    } else {
      var obj = {
        unionid: wx.getStorageSync('openid'),
      }
      var sign = app.sort_ASCII(obj)
      wx.request({
        url: app.globalData.new_url + '/assistant/Assistant/get_address',
        data: {
          unionid: wx.getStorageSync('openid'),
          sign: sign
        },
        method: 'POST',
        success: function(res) {
          console.log(res)
          if (res.data.code === 200) {
            that.setData({
              dikuang: false,
              address: res.data.content
            })
          }
        }
      })
    }
  },
  goindex: function() {
    let pages = getCurrentPages();
    let prevPage=pages[0]
    if (prevPage.route == "pages/tarbar/index/index"){
      prevPage.setData({
        isShowInterstitialAd: 100,
      })
    }
    wx.switchTab({
      url: '/pages/tarbar/index/index',
    })
  },
  //备注信息
  inpuevalue: function(e) {
    const that = this;
    that.setData({
      remarks: e.detail.value
    })
  },
  goAddress: function() {
    const that = this;
    that.setData({
      dikuang:true,
    })
    wx.navigateTo({
      url: '/pages/main/editAdd/index',
    })
  },
  signUps: function(e) {
    const that = this;
    var formid = e.detail.formId;
    var objform = {
      unionid: wx.getStorageSync('openid'),
      formid: formid,
      plate: 9,
    }
    var signform = app.sort_ASCII(objform)
    wx.request({
      url: app.globalData.new_url + '/user/user/add_formid',
      data: {
        unionid: wx.getStorageSync('openid'),
        formid: formid,
        plate: 9,
        sign: signform
      },
      method: 'POST',
      success: function(res) {
        console.log(res)
      }
    })
    var id = that.data.id;
    wx.showModal({
      title: '提示',
      content: '将消耗1热力币抽奖',
      success(res) {
        if (res.confirm) {
          var obj = {
            unionid: wx.getStorageSync('openid'),
            id: id,
            type: 2,
          }
          var sign = app.sort_ASCII(obj)
          wx.request({
            url: app.globalData.new_url + '/luck/luckydraw/sign_up',
            data: {
              unionid: wx.getStorageSync('openid'),
              id: id,
              type: 2,
              sign: sign
            },
            method: 'POST',
            success: function(res) {
              if (res.data.code === 200) {
                var objs = {
                  unionid: wx.getStorageSync('openid'),
                  luckyid: id,
                }
                var signs = app.sort_ASCII(objs)
                wx.request({
                  url: app.globalData.new_url + '/luck/Luckydraw/get_probability',
                  data: {
                    unionid: wx.getStorageSync('openid'),
                    luckyid: id,
                    sign: signs
                  },
                  method: 'POST',
                  success: function(res) {
                    console.log(res)
                    that.setData({
                      sort: res.data.content.sort,
                      freechou: true,
                      bmsuc: false,
                      clickedsuc: true,
                    },()=>{
                      that.onShow();
                    })
                  }
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  lotterydraw: function(e) {
    const that = this;
    var formid = e.detail.formId;
    var objform = {
      unionid: wx.getStorageSync('openid'),
      formid: formid,
      plate: 9,
    }
    var signform = app.sort_ASCII(objform)
    wx.request({
      url: app.globalData.new_url + '/user/user/add_formid',
      data: {
        unionid: wx.getStorageSync('openid'),
        formid: formid,
        plate: 9,
        sign: signform
      },
      method: 'POST',
      success: function(res) {
        console.log(res)
      }
    })
    if (that.data.ad_auth || that.data.video_ad) {
      that.setData({
        freechou: false,
      })
    } else if (that.data.currency) {
      var id = that.data.id;
      wx.showModal({
        title: '提示',
        content: '将消耗1热力币抽奖',
        success(res) {
          if (res.confirm) {
            var obj = {
              unionid: wx.getStorageSync('openid'),
              id: id,
              type: 2,
            }
            var sign = app.sort_ASCII(obj)
            wx.request({
              url: app.globalData.new_url + '/luck/luckydraw/sign_up',
              data: {
                unionid: wx.getStorageSync('openid'),
                id: id,
                type: 2,
                sign: sign
              },
              method: 'POST',
              success: function(res) {
                if (res.data.code === 200) {
                  var objs = {
                    unionid: wx.getStorageSync('openid'),
                    luckyid: id,
                  }
                  var signs = app.sort_ASCII(objs)
                  wx.request({
                    url: app.globalData.new_url + '/luck/Luckydraw/get_probability',
                    data: {
                      unionid: wx.getStorageSync('openid'),
                      luckyid: id,
                      sign: signs
                    },
                    method: 'POST',
                    success: function(res) {
                      console.log(res)
                      that.setData({
                        sort: res.data.content.sort,
                        bmsuc: false,
                        clickedsuc: true,
                      },()=>{
                        that.onShow();
                      })
                    }
                  })
                }
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      that.setData({
        norelibi: false,
      })
    }
  },
  editAddress: function(e) {
    var that = this;
    // wx.chooseAddress({
    //   success: function(res) {
    //     var address={};
    //     address.name = res.userName;
    //     address.phone = res.telNumber;
    //     address.address = res.provinceName+',' + res.cityName+',' + res.countyName
    //     address.detail_address = res.detailInfo;
    //     that.setData({
    //       address: address,
    //     })
    //   },
    //   fail: function(rs) {
    //     wx.getSetting({
    //       success(res) {
    //         if (!res.authSetting['scope.address']) {
    //           wx.showModal({
    //             title: '提示',
    //             content: '获取地址失败，请重新授权！',
    //             success: function(re) {
    //               if (re.confirm) {
    //                 wx.openSetting({
    //                   success: (res) => {
    //                     if (res.authSetting['scope.address']) {
    //                       wx.chooseAddress({
    //                         success: function(res) {
    //                           var address = {};
    //                           address.name = res.userName;
    //                           address.phone = res.telNumber;
    //                           address.address = res.provinceName + ',' + res.cityName + ',' + res.countyName
    //                           address.detail_address = res.detailInfo;
    //                           that.setData({
    //                             address: address
    //                           })
    //                         }
    //                       })
    //                     }
    //                   }
    //                 })
    //               }
    //             }
    //           })
    //         } else {
    //           return
    //         }
    //       }
    //     })
    //   },
    // })

  },
  clickshow: function() {
    const that = this;
    that.setData({
      showView: (!that.data.showView),
    })
  },
  gochoujiang: function() {
    wx.navigateTo({
      url: '/pages/other/choujiang/index',
    })
  },
  hideHandles: function() {
    const that = this;
    that.setData({
      bmsuc: true,
      clickedsuc: false,
    })
    that.data.flow = false;
  },
  hideHandle: function() {
    const that = this;
    that.setData({
      lqsuc: true,
      freechou: true,
      norelibi: true,
      luck: true,
      dikuang: true,
      zlfail: true,
      zlsuc: true,
      clickedfail: false,
      tryfail: true,
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(e) {
    const that = this;
    var share = e.target.dataset.share;
    var shareimg = that.data.content.share_img;
    var openid = wx.getStorageSync('openid');
    var title = that.data.content.first_prize;
    var id = that.data.id;
    return {
      title: `我正在抽${title}，快来帮我点，一起免费抽！`,
      imageUrl: shareimg,
      path: '/pages/tarbar/index/index?openid=' + openid + '&&id=' + id + '&&cj=123' + '&&share=' + share
    }
  }
})